home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / CONSTANT.JAV < prev    next >
Encoding:
Text File  |  1997-02-26  |  8.2 KB  |  345 lines

  1. /*
  2.  
  3.  * @(#)ConstantPoolInfo.java    1.5 95/08/16 Chuck McManis
  4.  
  5.  *
  6.  
  7.  * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  8.  
  9.  *
  10.  
  11.  * Permission to use, copy, modify, and distribute this software
  12.  
  13.  * and its documentation for NON-COMMERCIAL purposes and without
  14.  
  15.  * fee is hereby granted provided that this copyright notice
  16.  
  17.  * appears in all copies.
  18.  
  19.  *
  20.  
  21.  * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  22.  
  23.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  
  25.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  26.  
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
  28.  
  29.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  30.  
  31.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  32.  
  33.  */
  34.  
  35.  
  36.  
  37. package util;
  38.  
  39.  
  40.  
  41. import java.io.DataInputStream;
  42.  
  43. import java.io.DataOutputStream;
  44.  
  45. import java.io.IOException;
  46.  
  47.  
  48.  
  49. /**
  50.  
  51.  * This class defines an entry in the constant pool for a Java class.
  52.  
  53.  * The class file is primarily composed of ConstantPool entries and
  54.  
  55.  * manipulation is done by modifying those entries.
  56.  
  57.  *
  58.  
  59.  * @version     1.5, 16 Aug 1995
  60.  
  61.  * @author    Chuck McManis
  62.  
  63.  * @see        ClassFile
  64.  
  65.  */
  66.  
  67.  
  68.  
  69. public class ConstantPoolInfo{
  70.  
  71.     int    type;            // type of this item
  72.  
  73.     String name;         // String for the type
  74.  
  75.     ConstantPoolInfo  arg1;    // index to first argument
  76.  
  77.     ConstantPoolInfo  arg2;    // index to second argument
  78.  
  79.     short index1, index2;
  80.  
  81.     String     strValue;         // ASCIZ String value
  82.  
  83.     int        intValue;
  84.  
  85.     long    longValue;
  86.  
  87.     float    floatValue;
  88.  
  89.     double    doubleValue;
  90.  
  91.  
  92.  
  93.     public static final int CLASS = 7;
  94.  
  95.     public static final int FIELDREF = 9;
  96.  
  97.     public static final int METHODREF = 10;
  98.  
  99.     public static final int STRING = 8;
  100.  
  101.     public static final int INTEGER = 3;
  102.  
  103.     public static final int FLOAT = 4;
  104.  
  105.     public static final int LONG = 5;
  106.  
  107.     public static final int DOUBLE = 6;
  108.  
  109.     public static final int INTERFACE = 11;
  110.  
  111.     public static final int NAMEANDTYPE = 12;
  112.  
  113.     public static final int ASCIZ = 1;
  114.  
  115.     public static final int UNICODE = 2;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.     /**
  122.  
  123.      * Construct a new ConstantPoolInfo object that is of type ASCIZ
  124.  
  125.      */
  126.  
  127.     public ConstantPoolInfo(String value) {
  128.  
  129.     index1 = -1;
  130.  
  131.     index2 = -1;
  132.  
  133.     arg1 = null;
  134.  
  135.     arg2 = null;
  136.  
  137.     type = ASCIZ;
  138.  
  139.     strValue = value;
  140.  
  141.     }
  142.  
  143.  
  144.  
  145.     /**
  146.  
  147.      * Construct a new ConstantPoolInfo object that is of type INTEGER
  148.  
  149.      */
  150.  
  151.     public ConstantPoolInfo(int value) {
  152.  
  153.     index1 = -1;
  154.  
  155.     index2 = -1;
  156.  
  157.     arg1 = null;
  158.  
  159.     arg2 = null;
  160.  
  161.     type = INTEGER;
  162.  
  163.     intValue = value;
  164.  
  165.     }
  166.  
  167.  
  168.  
  169.     /**
  170.  
  171.      * Construct a new ConstantPoolInfo object that is of type FLOAT
  172.  
  173.      */
  174.  
  175.     public ConstantPoolInfo(float value) {
  176.  
  177.     index1 = -1;
  178.  
  179.     index2 = -1;
  180.  
  181.     arg1 = null;
  182.  
  183.     arg2 = null;
  184.  
  185.     type = FLOAT;
  186.  
  187.     floatValue = value;
  188.  
  189.     }
  190.  
  191.  
  192.  
  193.     /**
  194.  
  195.      * Construct a new ConstantPoolInfo object that is of type LONG
  196.  
  197.      */
  198.  
  199.     public ConstantPoolInfo(long value) {
  200.  
  201.     index1 = -1;
  202.  
  203.     index2 = -1;
  204.  
  205.     arg1 = null;
  206.  
  207.     arg2 = null;
  208.  
  209.     type = LONG;
  210.  
  211.     longValue = value;
  212.  
  213.     }
  214.  
  215.  
  216.  
  217.     /**
  218.  
  219.      * Construct a new ConstantPoolInfo object that is of type DOUBLE
  220.  
  221.      */
  222.  
  223.     public ConstantPoolInfo(double value) {
  224.  
  225.     index1 = -1;
  226.  
  227.     index2 = -1;
  228.  
  229.     arg1 = null;
  230.  
  231.     arg2 = null;
  232.  
  233.     type = DOUBLE;
  234.  
  235.     doubleValue = value;
  236.  
  237.     }
  238.  
  239.  
  240.  
  241.     /**
  242.  
  243.      * Generic constructor
  244.  
  245.      */
  246.  
  247.     public ConstantPoolInfo() {
  248.  
  249.     index1 = -1;
  250.  
  251.     index2 = -1;
  252.  
  253.     arg1 = null;
  254.  
  255.     arg2 = null;
  256.  
  257.     type = -1;
  258.  
  259.     }
  260.  
  261.  
  262.  
  263.     /**
  264.  
  265.      * return the type of this constant pool item.
  266.  
  267.      */
  268.  
  269.     public int isType() {
  270.  
  271.     return (type);
  272.  
  273.     }
  274.  
  275.  
  276.  
  277.     public boolean read(DataInputStream dis)
  278.  
  279.     throws IOException {
  280.  
  281.     int    len;
  282.  
  283.     char    c;
  284.  
  285.  
  286.  
  287.     type = dis.readByte();
  288.  
  289.     switch (type) {
  290.  
  291.         case CLASS:
  292.  
  293.         name = "Class";
  294.  
  295.         index1 = dis.readShort();
  296.  
  297.         index2 = -1;
  298.  
  299.         break;
  300.  
  301.         case FIELDREF:
  302.  
  303.         name = "Field Reference";
  304.  
  305.         index1 = dis.readShort();
  306.  
  307.         index2 = dis.readShort();
  308.  
  309.         break;
  310.  
  311.         case METHODREF:
  312.  
  313.         name = "Method Reference";
  314.  
  315.         index1 = dis.readShort();
  316.  
  317.         index2 = dis.readShort();
  318.  
  319.         break;
  320.  
  321.         case INTERFACE:
  322.  
  323.         name = "Interface Method Reference";
  324.  
  325.         index1 = dis.readShort();
  326.  
  327.         index2 = dis.readShort();
  328.  
  329.         break;
  330.  
  331.         case NAMEANDTYPE:
  332.  
  333.         name = "Name and Type";
  334.  
  335.         index1 = dis.readShort();
  336.  
  337.         index2 = dis.readShort();
  338.  
  339.         break;
  340.  
  341.         case STRING:
  342.  
  343.         name = "String";
  344.  
  345.         index1 = dis.readShort();
  346.  
  347.         index2 = -1;
  348.  
  349.         break;
  350.  
  351.         case INTEGER:
  352.  
  353.         name = "Integer";
  354.  
  355.         intValue = dis.readInt();
  356.  
  357.         break;
  358.  
  359.         case FLOAT:
  360.  
  361.         name = "Float";
  362.  
  363.         floatValue = dis.readFloat();
  364.  
  365.         break;
  366.  
  367.         case LONG:
  368.  
  369.         name = "Long";
  370.  
  371.         longValue = dis.readLong();
  372.  
  373.         break;
  374.  
  375.         case DOUBLE:
  376.  
  377.         name = "Double";
  378.  
  379.         doubleValue = dis.readDouble();
  380.  
  381.         break;
  382.  
  383.         case ASCIZ:
  384.  
  385.         case UNICODE:
  386.  
  387.         if (type == ASCIZ)
  388.  
  389.             name = "ASCIZ";
  390.  
  391.         else
  392.  
  393.             name = "UNICODE";
  394.  
  395.  
  396.  
  397.         StringBuffer xxBuf = new StringBuffer();
  398.  
  399.  
  400.  
  401.         len = dis.readShort();
  402.  
  403.         while (len > 0) {
  404.  
  405.             c = (char) (dis.readByte());
  406.  
  407.             xxBuf.append(c);
  408.  
  409.             len--;
  410.  
  411.         }
  412.  
  413.         strValue = xxBuf.toString();
  414.  
  415.         break;
  416.  
  417.         default:
  418.  
  419.         System.out.println("Warning bad type.");
  420.  
  421.     }
  422.  
  423.     return (true);
  424.  
  425.     }
  426.  
  427.  
  428.  
  429.     public void write(DataOutputStream dos, ConstantPoolInfo pool[])
  430.  
  431.     throws IOException, Exception {
  432.  
  433.     dos.write(type);
  434.  
  435.     switch (type) {
  436.  
  437.         case CLASS:
  438.  
  439.         case STRING:
  440.  
  441.         dos.writeShort(indexOf(arg1, pool));
  442.  
  443.         break;
  444.  
  445.         case FIELDREF:
  446.  
  447.         case METHODREF:
  448.  
  449.         case INTERFACE:
  450.  
  451.         case NAMEANDTYPE:
  452.  
  453.         dos.writeShort(indexOf(arg1, pool));
  454.  
  455.         dos.writeShort(indexOf(arg2, pool));
  456.  
  457.         break;
  458.  
  459.         case INTEGER:
  460.  
  461.         dos.writeInt(intValue);
  462.  
  463.         break;
  464.  
  465.         case FLOAT:
  466.  
  467.         dos.writeFloat(floatValue);
  468.  
  469.         break;
  470.  
  471.         case LONG:
  472.  
  473.         dos.writeLong(longValue);
  474.  
  475.         break;
  476.  
  477.         case DOUBLE:
  478.  
  479.         dos.writeDouble(doubleValue);
  480.  
  481.         break;
  482.  
  483.         case ASCIZ:
  484.  
  485.         case UNICODE:
  486.  
  487.         dos.writeShort(strValue.length());
  488.  
  489.         dos.writeBytes(strValue);
  490.  
  491.         break;
  492.  
  493.         default:
  494.  
  495.         throw new Exception("ConstantPoolInfo::write() - bad type.");
  496.  
  497.     }
  498.  
  499.     }
  500.  
  501.  
  502.  
  503.     public String toString() {
  504.  
  505.     StringBuffer s;
  506.  
  507.  
  508.  
  509.     if (type == ASCIZ) {
  510.  
  511.         return(strValue);
  512.  
  513.     }
  514.  
  515.  
  516.  
  517.     if (type == INTEGER) {
  518.  
  519.         return("= "+intValue);
  520.  
  521.     }
  522.  
  523.  
  524.  
  525.     if (type == LONG) {
  526.  
  527.         return("= "+longValue);
  528.  
  529.     }
  530.  
  531.  
  532.  
  533.     if (type == FLOAT) {
  534.  
  535.         return("= "+floatValue);
  536.  
  537.     }
  538.  
  539.  
  540.  
  541.     if (type == DOUBLE) {
  542.  
  543.         return("= "+doubleValue);
  544.  
  545.     }
  546.  
  547.  
  548.  
  549.     s = new StringBuffer();
  550.  
  551.     s.append(name);
  552.  
  553.     s.append(":");
  554.  
  555.     if (arg1 != null)
  556.  
  557.         s.append(arg1.toString());
  558.  
  559.     else if (index1 != -1)
  560.  
  561.         s.append("I1["+index1+"], ");
  562.  
  563.     if (arg2 != null)
  564.  
  565.         s.append(arg2.toString());
  566.  
  567.     else if (index2 != -1)
  568.  
  569.         s.append("I2["+index2+"], ");
  570.  
  571.     return (s.toString());
  572.  
  573.     }
  574.  
  575.  
  576.  
  577.     public static short indexOf(ConstantPoolInfo item,
  578.  
  579.                     ConstantPoolInfo pool[])
  580.  
  581.     throws Exception {
  582.  
  583.     for (int i = 0; i < pool.length; i++) {
  584.  
  585.         if (item == pool[i])
  586.  
  587.         return (short) i;
  588.  
  589.     }
  590.  
  591.     throw new Exception("ConstantPoolInfo:: indexOf() - item not in pool.");
  592.  
  593.     }
  594.  
  595.  
  596.  
  597.     /**
  598.  
  599.      * Returns true if these constant pool items are identical.
  600.  
  601.      */
  602.  
  603.     public boolean isEqual(ConstantPoolInfo cp) {
  604.  
  605.     if (cp == null)
  606.  
  607.         return false;
  608.  
  609.  
  610.  
  611.     if (cp.type != type)
  612.  
  613.         return (false);
  614.  
  615.     switch (cp.type) {
  616.  
  617.         case CLASS:
  618.  
  619.         case STRING:
  620.  
  621.         return (arg1 == cp.arg1);
  622.  
  623.         case FIELDREF:
  624.  
  625.         case METHODREF:
  626.  
  627.         case INTERFACE:
  628.  
  629.         case NAMEANDTYPE:
  630.  
  631.         return ((arg1 == cp.arg1) && (arg2 == cp.arg2));
  632.  
  633.         case INTEGER:
  634.  
  635.         return (cp.intValue == intValue);
  636.  
  637.         case FLOAT:
  638.  
  639.         return (cp.floatValue == floatValue);
  640.  
  641.         case LONG:
  642.  
  643.         return (cp.longValue == longValue);
  644.  
  645.         case DOUBLE:
  646.  
  647.         return (cp.doubleValue == doubleValue);
  648.  
  649.         case ASCIZ:
  650.  
  651.         case UNICODE:
  652.  
  653.         return (cp.strValue.compareTo(strValue) == 0);
  654.  
  655.     }
  656.  
  657.     return (false);
  658.  
  659.     }
  660.  
  661.  
  662.  
  663.     /**
  664.  
  665.      * Returns the reference to the constant pool item that is
  666.  
  667.      * already in pool, that matches this one.
  668.  
  669.      */
  670.  
  671.     public ConstantPoolInfo inPool(ConstantPoolInfo pool[]) {
  672.  
  673.     for (int i = 1; i < pool.length; i++) {
  674.  
  675.         if (isEqual(pool[i]))
  676.  
  677.         return (pool[i]);
  678.  
  679.     }
  680.  
  681.     return null;
  682.  
  683.     }
  684.  
  685.  
  686.  
  687. }
  688.  
  689.